require "import"
import "str"

local function sponsorDialog(enabled,shopId,months)
 if enabled==true then
            local edt=AlertDialog.Builder(this)
            -- .setCancelable(false)
            .setTitle("选择赞助支付方式")
            .setItems({"微信","支付宝","QQ"}, function (d,n)
              if n==1 then
                payfs="alipay"
               elseif n==2 then
                payfs="qqpay"
               elseif n==0 then
                payfs="wxpay"
                toast("微信支付请复制链接发给自己然后点击链接支付")
              end
              购买商品(t1url,t1key,payfs,os.time()*100+math.random(1,99),"", tostring (shopId),[[{"user":"]]..activity.getSharedData("username")..[[","num":"]]..months..[["}]],t1httk)
            end)
            local ed=edt.show()
            local pw=ed.getWindow()
            --.setWindowAnimations(R.style.BottomDialog_Animation)
            --.setBackgroundDrawable(ColorDrawable(0))
            .setGravity(Gravity.BOTTOM)
            圆角(pw,背景色,{0,0,0,0,0,0,0,0})
            local lp=pw.getAttributes()
            lp.width=w
            pw.setAttributes(lp)
            pw.setDimAmount(0.35)
           elseif enabled==false then
            toast("已下架")
           else
            toast("请等待价格获取完成")
          end
end

this.setContentView(loadlayout ({
  RelativeLayout,
  --layout_width="fill",
  --layout_height="fill",
  --visibility=4,
  backgroundColor=背景色,
  --orientation="vertical",
  --elevation="2%w",
  paddingTop=状态栏高度,
  {
    RelativeLayout,
    layout_width="fill",
    layout_height="56dp" ,
    gravity="center",
    padding="16dp",
    --backgroundColor=0xffffffff,
    elevation="2dp",
    paddingTop="8dp",
    paddingBottom="8dp",
    {
      ImageView,
      src="drawable/back.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      onClick=function ()
        this.finish()
      end,
      colorFilter=图标色,
      padding="9dp",
    },
    {
      TextView,
      layout_width="fill",
      layout_height="fill",
      gravity="center",
      --layout_gravity="center",
      text="Sup会员",
      textSize="20sp",
      textColor=文字色,
    },
  },
  {
    ScrollView,
    layout_width="fill",
    {
      LinearLayout,
      layout_width="fill",
      orientation="vertical",
      paddingBottom="16dp",
      {
        LinearLayout,
        paddingTop="56dp" ,
      },
      {
        TextView,
        text="我的会员状态：获取中",
        textSize="16sp",
        textColor=文字色,
        layout_gravity="center",
        padding="16dp",
       -- layout_height="-2",
        gravity="center",
        id="myinfo"
      },
      
      {
      LinearLayout,
      layout_gravity=17,
      {
      LinearLayout,
      gravity=17,
      layout_gravity=17,
      padding="12dp",
      foreground=波纹(波纹色),
      onClick=function()
      onStart(true)
      end,
      {
      ImageView,
      layout_width="50dp",
      layout_height="20dp",
      layout_marginRight="8dp",
      colorFilter=图标色,
      src="drawable/refresh.png",
      },
      {
        TextView,
        text="点击签到",
        textSize="14sp",
        textColor=文字色,
        id="is_qd"
      },
     
      },                
        {
        
        
        TextView,
        
        layout_width="150dp",
      layout_height="25dp",
      layout_marginLeft="30dp",
      layout_gravity="right|center",
        text="当前积分:获取中",
        textSize="14sp",
        textColor=文字色,
        id="qd_jf"



      },
},


      sort("赞助支持",nil,"sponsorTitle"),
    
    
    
    {
   LinearLayout,
   layout_width=-1,
   orientation=1,
   visibility=8,
   id="sponsorOfflineArea",
   
   {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
           sponsorDialog(one_month_enabled,1,1)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="一个月：获取价格中",
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
          id="one_month_info";
        },
        {
          ImageView,
          colorFilter=图标色,
          src="drawable/arrow.png",
          rotation=-90,
          --layout_alignParentRight=true,
          layout_width="24dp",
          layout_height="24dp",
          layout_gravity="center|right",
        },
      },
      {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function ()
          sponsorDialog(three_month_enabled,2,3)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="三个月：获取价格中",
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
          id="three_month_info";
        },
        {
          ImageView,
          colorFilter=图标色,
          src="drawable/arrow.png",
          rotation=-90,
          --layout_alignParentRight=true,
          layout_width="24dp",
          layout_height="24dp",
          layout_gravity="center|right",
        },
      },
      {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function ()
            sponsorDialog(one_year_enabled,3,12)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="一年：获取价格中",
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
          id="one_year_info";
        },
        {
          ImageView,
          colorFilter=图标色,
          src="drawable/arrow.png",
          rotation=-90,
      --    layout_alignParentRight=true,
          layout_width="24dp",
          layout_height="24dp",
          layout_gravity="center|right",
        },
      },
      
   },

{
    TextView,
    layout_width=-1,
    minimumHeight="120dp",
    textSize="14sp",
    gravity=17,
    visibility=8,
    text="暂未开放赞助",
    id="sponsorTip",
    textColor=次要文字色,
 },
 
 {
    TextView,
    layout_width=-1,
    minimumHeight="120dp",
    textSize="14sp",
    gravity=17,
  --  visibility=8,
    text="正在加载",
    id="sponsorLoading",
    textColor=次要文字色,
 },
 
 {
   LinearLayout,
   layout_width=-1,
   visibility=8,
   orientation=1,
   id="sponsorArea",
   
 },

      sort("近期公告"),

      {
        FrameLayout,
      -- layout_height=lay_wh,
        layout_width="fill",
        --padding="10dp",
        layout_gravity="center",
        {
          TextView,
        --  text="公告：优化加载中...若长时间未显示，请清除缓存后重试.",
          textSize="14sp",
          textColor=文字色,
          layout_gravity="center",
         -- layout_height="fill",
          gravity="center",
          id="announcement";
        },
      },


      {
        TextView,
        text="",
        textSize="14sp",
        textColor=次要文字色,
        layout_gravity="center",
      padding="6dp",
        --layout_height="-2",
        gravity="center",
        id="infom"
      },
      sort("更多"),


      {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
          showDialog("我们在业余时间设计出闲言APP","        点击签到即可获取一天Sup会员，只要每天上线签到都能得到会员，每天签到一次可获得1积分 \n       闲言Sup会员权益:\n⊙1.解锁 垂直领域 所有功能 \n⊙2.云端字体下载\n⊙3.高清图片下载 \n闲言的会员作用很小，我们希望能有更多人使用我们的软件 \n闲言APP内容资源均来自网络，软件仅收集整理。"
          ,"返回主页","我知道了",function ()
            print("你的支持将会给开发者带来积极性")
            activity.finish()

          end,nil,0xffff4500,nil,true,nil,nil,nil)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="如何获取会员",
          textSize="16sp",
          textColor="0xff3277c2",
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },

      },


      {
        FrameLayout,
      --   layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
          showDialog("一个公益性质的软件","      这是一个全新轻量化的软件，大小仅4M\n采用纯净后台运行方式，减少手机运行负担，能最大程度的将文字内容发挥最大用途。 \n     这一年来，我们一直在开发软件和学习更多代码来维护软件，尽管软件存在很多问题，我们会长期维护和更新这个绿色软件。\n     在软件里面没有任何广告和推广的情况下，你的一个5星好评都能成为激发我们更新维护软件的动力！\n如果你在软件使用发现问题，请及时反馈，\n邮箱 5147662@qq.com     \n      一个公益性质的安卓软件，没有任何广告，所有的赞助将用来完善开发环境，为用户提供更好的功能服务\n    开发者:微风暴网络科技工作室\n软件设计:lzyang   & 无书先生\n", "返回主页","我知道了",function ()

            activity.finish()
          end,nil,0xffff4500,nil,true,nil,nil,nil)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="这是什么软件？",
          textSize="16sp",
          textColor="0xff3277c2",
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },

      },


      {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
          showDialog("感谢您在百忙中选择了我们的软件","         在您体验闲言App的期间，\n如果您对我们提供的服务不满意，\n您可以向我们发起反馈或者投诉，\n此过程需要提供您的闲言账号和手机型号和软件相关信息，我们会在3个工作日内尝试与您取得联系，并与您一起解决您所遇到的问题。\n           感谢您对我们软件服务的监督，我们会把软件做得更好！        ", "提交投诉单","我知道了",function ()

            InAppBrowser("http://zt.agyer.xyz/app/liuyan/xytk/index.php")
          end,nil,0xffff4500,nil,true,nil,nil,nil)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="对软件不满意怎么办？",
          textSize="16sp",
          textColor="0xff3277c2",
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },

      },

      sort("账户"),


      {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
          activity.newActivity("change_password")
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="修改密码",
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },

      },

{
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
          activity.newActivity("app/more/cloud_note")
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="修改个性签名",
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },

      },
      
      {
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        layout_gravity="center",
        onClick=function()
          showDialog("您的账号可能至关重要！","        一旦注销成功，该账号将不再存在！\n现在注销，您的登录状态会消失，本地用户信息会被清除，若要永久删除此账号，需填写用户账号、用户积分、常用登录地、注销原因等，\n但是您的收藏和历史记录会一直保存在本地。\n后期您可通过闲言官网查询您的账号注销进度，若无法查询账号，则说明注销失败，须重新申请注销", "申请注销","取消",function ()
            activity.setSharedData("username",nil)
            activity.setSharedData("password",nil)
            activity.setSharedData("vip_time",nil)
            InAppBrowser("http://zt.agyer.xyz/app/liuyan/xytk/index.php")
            activity.getActivity("soup").call("退出账号")
            activity.finish()
          end,nil,0xffff4500,nil,true,nil,nil,nil)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="删除账号",
          textSize="16sp",
          textColor="#F44336",
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },

      },

    },
  },
  {
    LinearLayout,
    layout_width="fill",
    layout_height="56dp" ,
    foreground=上下渐变({背景色,深透,淡透}),
  },
}))

function setSponsorTipVisible(group)
if group.visibility!=0 then
sponsorTip.setVisibility(8)
end
local v=0
local c=group.getChildCount()
for k=0,c-1 do
v=v+group.getChildAt(k).visibility
end
sponsorTip.setVisibility(c*8==v&&0||8)
end

local function getLocalSponsor()
Http.get(
--"http://i.aixinbiji.com/user_all.json?key="..t1key.."&type=get_goods_info&goodsid=1&token="..MD5(t1key.."1"..t1httk),
getApiUrl("i","user_all.json?key="..t1key.."&type=get_goods_info&goodsid=1&token="..MD5(t1key.."1"..t1httk)),
function(code,body,cookie,headers)
  if code==200 then
    data=cjson.decode(body)
    if data["code"]=="1" then
      if data["zt"]=="下架" then
one_month_info.parent.visibility=8
        one_month_enabled=false
     --   one_month_info.Text="一个月：已下架"
       else
one_month_info.parent.visibility=0
      one_month_info.Text="赞助一个月："..data["money"]
        one_month_enabled=true
      end
     elseif data["code"]=="0" then
     one_month_info.parent.visibility=8
      toast(data["message"])
    end
   else
    one_month_info.parent.visibility=8
    toast("出现异常！")
  end
setSponsorTipVisible(sponsorOfflineArea)
end)

Http.get(
--"http://i.aixinbiji.com/user_all.json?key="..t1key.."&type=get_goods_info&goodsid=2&token="..MD5(t1key.."2"..t1httk),
getApiUrl("i","user_all.json?key="..t1key.."&type=get_goods_info&goodsid=2&token="..MD5(t1key.."2"..t1httk)),
function(code,body,cookie,headers)
  if code==200 then
    data=cjson.decode(body)
    if data["code"]=="1" then
         if data["zt"]=="下架" then
        three_month_enabled=false
three_month_info.parent.visibility=8
        three_month_info.Text="三个月：已下架"
       else
three_month_info.parent.visibility=0
       three_month_info.Text="赞助三个月："..data["money"]
    three_month_enabled=true
      end
     elseif data["code"]=="0" then
      toast(data["message"])
      three_month_info.parent.visibility=8
    end
   else
    toast("出现异常！")
    three_month_info.parent.visibility=8
  end
setSponsorTipVisible(sponsorOfflineArea)
end)

Http.get(
--"http://i.aixinbiji.com/user_all.json?key="..t1key.."&type=get_goods_info&goodsid=3&token="..MD5(t1key.."3"..t1httk)
getApiUrl("i","user_all.json?key="..t1key.."&type=get_goods_info&goodsid=3&token="..MD5(t1key.."3"..t1httk)),
function(code,body,cookie,headers)
--toast(body)
  if code==200 then
    data=cjson.decode(body)
    if data["code"]=="1" then
      if data["zt"]=="下架" then
one_year_info.parent.visibility=8
        one_year_enabled=false
        one_year_info.Text="一年：已下架"
       else
one_year_info.parent.visibility=0
       one_year_info.Text="赞助一年："..data["money"]
       one_year_enabled=true
      end
     elseif data["code"]=="0" then
one_year_info.parent.visibility=8
      toast(data["message"])
    end
   else
one_year_info.parent.visibility=8
    toast("出现异常！")
  end
setSponsorTipVisible(sponsorOfflineArea)
end)
end

function getSponsor()

Http.get("http://zt.agyer.xyz/app/xianyan/tuisong/sup.html", function(code, body)
  if code == 200 and body then
    local body = utf8.match(body,"%[公告%](.-)%[公告%]")
    announcement.setText(body)
   else
    announcement.setText("公告获取失败:"..code)
  end
end)

云文档获取(t1url,t1key,"rxexxa",function(返回值)
  if 返回值=="401" then
   else
    data=cjson.decode(返回值)
    if data["code"]=="1" then
      infom.Text=Html.fromHtml(data["text"])
    end
  end
end)

  
Http.get("http://zt.agyer.xyz/app/xianyan/tp/shop.json", function(c,n)
  if c!=200 then
getLocalSponsor()
    return
  end
--this.setDebug(true)
  n=cjson.decode(n)
  sponsorOfflineArea.visibility=8
  sponsorArea.visibility=0
  sponsorArea.removeAllViews()
  
sponsorLoading.visibility=8
  if n.display=="sponsor" then
sponsorTitle.text="赞助商"

for k,v in ipairs(n.sponsor) do
sponsorArea.addView(loadlayout{
        FrameLayout,
        --layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
       -- layout_gravity="center",
        onClick=function ()
          InAppBrowser(v.url)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text=v.name,
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
        },
        {
          ImageView,
          colorFilter=图标色,
          src="drawable/arrow.png",
          rotation=-90,
          --layout_alignParentRight=true,
          layout_width="26dp",
          layout_height="26dp",
          layout_gravity="center|right",
        },
      })
end
else
sponsorTitle.text="赞助支持"

--this.setDebug(true)
for k,v in ipairs(n.vip) do
local id={}
local enabled,month
    sponsorArea.addView(loadlayout({
        FrameLayout,
        -- layout_height=lay_wh,
        layout_width="fill",
        padding="16dp",
        --layout_gravity="center",
        onClick=function ()
            sponsorDialog(enabled,v,months)
        end,
        foreground=波纹(波纹色),
        {
          TextView,
          text="获取价格中",
          textSize="16sp",
          textColor=文字色,
          layout_gravity="left|center",
          layout_height="fill",
          gravity="center",
          id="info";
        },
        {
          ImageView,
          colorFilter=图标色,
          src="drawable/arrow.png",
          rotation=-90,
      --    layout_alignParentRight=true,
          layout_width="24dp",
          layout_height="24dp",
          layout_gravity="center|right",
        },
      },id))
     Http.get(
--"http://i.aixinbiji.com/user_all.json?key="..t1key.."&type=get_goods_info&goodsid=3&token="..MD5(t1key.."3"..t1httk)
getApiUrl("i","user_all.json?key="..t1key.."&type=get_goods_info&goodsid="..v.."&token="..MD5(t1key..v..t1httk)),
function(code,body,cookie,headers)
--toast(body)
enabled=false
  if code==200 then
    data=cjson.decode(body)
    if data["code"]=="1" then
local title=data.title
local m=title:match("[%d]+")
months=utf8.sub(title,-1)=="天"
&&tointeger(m/31)||m
--print(months)
      if data["zt"]=="下架" then
id.info.parent.visibility=8
        
        id.info.Text=title.."：已下架"
       else
id.info.parent.visibility=0
   id.info.Text="赞助 "..months.." 个月："..data["money"]
       enabled=true
      end
     elseif data["code"]=="0" then
id.info.parent.visibility=8
      toast(data["message"])
    end
   else
id.info.parent.visibility=8
    toast("出现异常！")
  end
setSponsorTipVisible(sponsorArea)
end)
end
end
end)

end

getSponsor()

function onStart(ref)
  if activity.getSharedData("password")~=nil then
    登录账号(t1url,t1key,activity.getSharedData("username"),activity.getSharedData("password"),"",t1httk,function(返回值)
      if 返回值=="401" then
        toast("用户状态异常！")
       else
        data=cjson.decode(返回值)
        if data["code"]=="1" then
          if ref then
          
          
每日签到(t1url,t1key,activity.getSharedData("username"),activity.getSharedData("password"),t1httk,function(返回值)
if 返回值=="401" then
toast("签到失败")
return
end
local data=cjson.decode(返回值)
onStart()
toast(data.message)
end)
         --  toast("已签到")
    
            
          end


if data.isqd=="未签到" then
qd_jf.Text="点击签到"
           else

is_qd.Text=data["isqd"]
          activity.setSharedData("isqd",data["isqd"])

          end



qd_jf.Text="用户积分："..data["jf"]
          activity.setSharedData("jf",data["jf"])
        
          
          myinfo.Text="我的会员状态："..data["vip_time"]
          activity.setSharedData("vip_time",data["vip_time"])
          if this.getPackageName():find"and"==nil then
       -- this.setDebug(true)
        local ids=this.getSystemService("appwidget")
        .getAppWidgetIds(luajava.bindClass"android.content.ComponentName"(this,"com.windmill.widget.OneWordWidgetProvider"))
this.sendBroadcast(
Intent()
.setAction("android.appwidget.action.APPWIDGET_UPDATE")
.setClass(this--[[.getApplication()]],
luajava.bindClass"com.windmill.widget.OneWordWidgetProvider"
)
.putExtra("appWidgetIds",ids)
)
end
         elseif data["code"]=="0" then
          toast(data["message"])
        end
        
                       --is_qd  签到状态
                       --qd_jf  签到积分
        
      end
    end)
  end
end
